package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.OrderDao;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.entity.OrderEntity;
import io.renren.modules.app.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther zxl
 * @Date 2018/12/17 13:50
 * @Description
 **/
@Service("OrderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    OrderDao orderDao;
    @Autowired
    UserDao userDao;

    @Override
    public boolean addOrder(OrderEntity orderEntity) {

        orderEntity.setCreateTime(new Date());
        boolean b = false;
        b =this.insert(orderEntity);
        if (b){
            userDao.updateUserState(2,orderEntity.getUserId());
        }

        return b;
    }

    @Override
    public List<OrderEntity> selectOrders(Long userId) {

        return
                baseMapper.selectList(new EntityWrapper<OrderEntity>()
                .where("user_id={0}",userId).orderBy("create_time"));

    }

    @Override
    public List<OrderEntity> selectOrderNotClose(Long userId) {

        return baseMapper.selectList(new EntityWrapper<OrderEntity>()
                .where("user_id={0}",userId).orderBy("create_time")
                .and()
                .lt("order_state",5)
                .or()
                .eq("order_state",10)
        );

    }

    @Override
    public boolean updateOrderAccess(OrderEntity orderEntity) {

        Integer res = orderDao.updateAssess(orderEntity);
        if (res>0){
            userDao.updateUserState(5,orderEntity.getUserId());
            return true;
        }
        return false;
    }

    @Override
    public boolean updateOrderReceiver(OrderEntity orderEntity) {

        Integer res = orderDao.updateReceiver(orderEntity);
        if (res>0){
            return true;
        }
        return false;
    }

    @Override
    public OrderEntity getOneByOrderId(String orderId) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderId(orderId);
        return baseMapper.selectOne(orderEntity);

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String orderId = (String) params.get("orderId");
        String userId = (String) params.get("userId");
        String agentId = (String) params.get("agentId");

        List list = new ArrayList<String>(1);
        list.add("create_time");
        Page<OrderEntity> page;
        if (orderId == null && userId == null && agentId == null){

            page = this.selectPage(
                    new Query<OrderEntity>(params).getPage(),
                    new EntityWrapper<OrderEntity>()
                            .orderDesc(list)
            );

        }else {
            page = this.selectPage(
                    new Query<OrderEntity>(params).getPage(),
                    new EntityWrapper<OrderEntity>()
                            .like(StringUtils.isNotBlank(userId),"user_id",userId)
                            .like(StringUtils.isNotBlank(orderId),"order_id",orderId)
                            .like(StringUtils.isNotBlank(agentId),"agent_id",agentId)
            );
        }
        return new PageUtils(page);
    }

    @Override
    public boolean updateOrderState(String orderId, Integer state) {

        Integer res = orderDao.updateOrderState(orderId,state);

        if (res!=null&&res>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean rejectOrder(String orderId, String reason) {

        Integer res = orderDao.rejectOrder(orderId,reason);
        if (res != null && res>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean cancelOrder(String orderId) {

        Integer res = orderDao.updateOrderState(orderId,8);

        return res>0;
    }

    @Override
    public boolean successOrder(String orderId,Long userId) {

        boolean b = orderDao.successOrder(orderId)>0;
        if (b){
            userDao.updateUserState(5,userId);
        }
        return b;
    }

    @Override
    public boolean updateOrder(OrderEntity orderEntity) {

        Integer res = baseMapper.update(orderEntity,new EntityWrapper<OrderEntity>()
                .where("order_id={0}",orderEntity.getOrderId()));

        return res>0;
    }


}
